package com.zeta.eduservice.service.impl;

import com.zeta.eduservice.entity.EduTeacher;
import com.zeta.eduservice.entity.UcenterMember;
import com.zeta.eduservice.entity.question.EduQuestion;
import com.zeta.eduservice.entity.question.EduQuestionChoice;
import com.zeta.eduservice.entity.question.QuestionFile;
import com.zeta.eduservice.entity.vo.QuestionUploadVo;
import com.zeta.eduservice.entity.vo.WordUploadInfo;
import com.zeta.eduservice.mapper.EduQuestionMapper;
import com.zeta.eduservice.mapper.QuestionFileMapper;
import com.zeta.eduservice.service.EduCoursequestionlistInfoService;
import com.zeta.eduservice.service.EduQuestionChoiceService;
import com.zeta.eduservice.service.EduQuestionService;
import com.zeta.eduservice.service.QuestionFileService;
import com.zeta.eduservice.utils.Download;
import com.zeta.eduservice.utils.WordAnalysisUtils;
import com.zeta.exceptionhandler.GuliException;
import com.zeta.utils.JwtUtils;

import io.netty.util.internal.ResourcesUtil;

import com.alibaba.excel.write.ExcelBuilder;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.Session;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 题文件表 服务实现类
 * </p>
 *
 * @author zeta
 * @since 2020-09-05
 */
@Service
public class QuestionFileServiceImpl extends ServiceImpl<QuestionFileMapper, QuestionFile> implements QuestionFileService {
	@Autowired
	EduCoursequestionlistInfoService eduCoursequestionlistInfoService;

	@Autowired
	WordAnalysisUtils wordAnaUtils;
	@Autowired
	Download downUtil;
	@Autowired
	private QuestionFileService questionService;
	@Autowired
	private EduQuestionService QuestionService;
	@Autowired
	private EduQuestionChoiceService QuestionChoiceService;
	@Autowired
	EduQuestionMapper questionMapper;
	@Override
	public void userUpload(MultipartFile file,HttpServletRequest request) {
		String user =JwtUtils.getMemberIdByJwtToken(request);
		String fileName=file.getOriginalFilename();
		String extension = "."+FilenameUtils.getExtension(file.getOriginalFilename());
		String newName=new SimpleDateFormat("yyyyMMddHH")+UUID.randomUUID().toString().replace("-","")+extension;
		Integer size = (int) file.getSize();
		String Type = file.getContentType();
		String dateFormat=new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		String realpath="C:\\Users\\Administrator\\Desktop\\TestDB-SpringBoot\\TestDB_parent\\file\\"+dateFormat;
		File dest = new File(realpath,newName);
		  if (!dest.getParentFile().exists()){
	            dest.getParentFile().mkdirs();
	        }
		try {
			file.transferTo(dest);
			QuestionFile questionFile = new QuestionFile();
			questionFile.setExt(extension)
			.setName(fileName)
			.setNewname(newName)
			.setSize(size)
			.setType(Type)
			.setPath(realpath)//+dateFormat
			.setUserid(user)
			.setFolderid("1");
			
			questionService.save(questionFile);
		} catch (IllegalStateException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public void download(String id, HttpServletResponse response) throws IOException{
		
	
		
	}
	@Override
	public void userWordload(MultipartFile file, HttpServletRequest request,QuestionUploadVo  chapterId) throws IOException {
//		String user =JwtUtils.getMemberIdByJwtToken(request);
//		String fileName=file.getOriginalFilename();
//		String extension = "."+FilenameUtils.getExtension(file.getOriginalFilename());
//		String newName=new SimpleDateFormat("yyyyMMddHH")+UUID.randomUUID().toString().replace("-","")+extension;
//		Integer size = (int) file.getSize();
//		String Type = file.getContentType();
//		
////		String dateFormat=new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//		String realpath="C:\\Users\\Administrator\\Desktop\\TestDB-SpringBoot\\TestDB_parent\\file\\questionFolderDownload\\"+wordInfo.getTeacherid();
//		File dest = new File(realpath,newName);
//		  if (!dest.getParentFile().exists()){
//	            dest.getParentFile().mkdirs();
//	        }
//		try {
//			file.transferTo(dest);
//			QuestionFile questionFile = new QuestionFile();
//			questionFile.setExt(extension)
//			.setName(fileName)
//			.setNewname(newName)
//			.setSize(size)
//			.setType(Type)
//			.setPath(realpath)//+dateFormat
//			.setUserid(user)
//			.setFolderid(wordInfo.getFolderid());
//			
			//questionService.save(questionFile);
		InputStream is = file.getInputStream();
		HWPFDocument doc = new HWPFDocument(is);
		Range range = doc.getRange();
		wordAnaUtils.AnalysisWord(range,chapterId);
		List<EduQuestionChoice> choiceQuestionLists = wordAnaUtils.ChoiceQuestionLists;
		List<EduQuestion> questionLists = wordAnaUtils.QuestionLists;
		if(choiceQuestionLists!=null) {
			QuestionChoiceService.saveBatch(choiceQuestionLists);
		}
		if(QuestionChoiceService!=null) {
			QuestionService.saveBatch(questionLists);
		}
		eduCoursequestionlistInfoService.updateCourseChoiceQuestionListInfo(chapterId);
		 List<String> typelist = questionMapper.getType(chapterId.getTypeId());
		eduCoursequestionlistInfoService.updateCourseQuestionListInfo(chapterId.getTypeId(), typelist);

		
//		} catch (IllegalStateException | IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}		
	}
}
